﻿namespace OleDbDAL.Sales
{
    using DBUtility;
    using IDal.Sales;
    using Model.Sales;
    using System;
    using System.Data;
    using System.Text;

    public class OrderPaymentApp : IOrderPayment
    {
        private string dbname = "Systems";

        public void Add(OrderPaymentEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_OrderPayment(");
            builder.Append("PaymentNum,GuestId,ChainNum,AllMoney,AccountId,PayTime,CreateMan,CreateTime,BallotType,PayType,Cost,TIN,BankName,BankNum,IsInvoicing,InvoicingNum,IsCheck,CheckTime,CheckMan,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.PaymentNum + "',");
            builder.Append("" + model.GuestId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("" + model.AllMoney + ",");
            builder.Append("" + model.AccountId + ",");
            builder.Append("'" + model.PayTime + "',");
            builder.Append("" + model.CreateMan + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.BallotType + ",");
            builder.Append("" + model.PayType + ",");
            builder.Append("" + model.Cost + ",");
            builder.Append("'" + model.TIN + "',");
            builder.Append("'" + model.BankName + "',");
            builder.Append("'" + model.BankNum + "',");
            builder.Append("" + model.IsInvoicing + ",");
            builder.Append("'" + model.InvoicingNum + "',");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Check(OrderPaymentEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_OrderPayment set ");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("IsCheck=" + model.IsCheck + "");
            builder.Append(" where PaymentId=" + model.PaymentId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int PaymentId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_OrderPayment ");
            builder.Append(" where PaymentId=" + PaymentId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_OrderPayment ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by PaymentId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "PaymentId", "T_OrderPayment", swhere);
        }

        public OrderPaymentEnt GetModel(int PaymentId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PaymentId,PaymentNum,GuestId,ChainNum,AllMoney,AccountId,PayTime,CreateMan,CreateTime,BallotType,PayType,Cost,TIN,BankName,BankNum,IsInvoicing,InvoicingNum,IsCheck,CheckTime,CheckMan,Remark from T_OrderPayment ");
            builder.Append(" where PaymentId=" + PaymentId);
            OrderPaymentEnt ent = new OrderPaymentEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.PaymentId = PaymentId;
            if (table.Rows.Count > 0)
            {
                ent.PaymentNum = table.Rows[0]["PaymentNum"].ToString();
                if (table.Rows[0]["GuestId"].ToString() != "")
                {
                    ent.GuestId = int.Parse(table.Rows[0]["GuestId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                if (table.Rows[0]["AllMoney"].ToString() != "")
                {
                    ent.AllMoney = decimal.Parse(table.Rows[0]["AllMoney"].ToString());
                }
                if (table.Rows[0]["AccountId"].ToString() != "")
                {
                    ent.AccountId = int.Parse(table.Rows[0]["AccountId"].ToString());
                }
                if (table.Rows[0]["PayTime"].ToString() != "")
                {
                    ent.PayTime = DateTime.Parse(table.Rows[0]["PayTime"].ToString());
                }
                if (table.Rows[0]["CreateMan"].ToString() != "")
                {
                    ent.CreateMan = int.Parse(table.Rows[0]["CreateMan"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["BallotType"].ToString() != "")
                {
                    ent.BallotType = int.Parse(table.Rows[0]["BallotType"].ToString());
                }
                if (table.Rows[0]["PayType"].ToString() != "")
                {
                    ent.PayType = int.Parse(table.Rows[0]["PayType"].ToString());
                }
                if (table.Rows[0]["Cost"].ToString() != "")
                {
                    ent.Cost = decimal.Parse(table.Rows[0]["Cost"].ToString());
                }
                ent.TIN = table.Rows[0]["TIN"].ToString();
                ent.BankName = table.Rows[0]["BankName"].ToString();
                ent.BankNum = table.Rows[0]["BankNum"].ToString();
                if (table.Rows[0]["IsInvoicing"].ToString() != "")
                {
                    ent.IsInvoicing = int.Parse(table.Rows[0]["IsInvoicing"].ToString());
                }
                ent.InvoicingNum = table.Rows[0]["InvoicingNum"].ToString();
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_OrderPayment ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "PaymentId");
        }

        public DataTable ReturnTopReturn(string strWhere, int top)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select top " + top.ToString() + " * from T_OrderPayment ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by PaymentId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public void Update(OrderPaymentEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_OrderPayment set ");
            builder.Append("PaymentNum='" + model.PaymentNum + "',");
            builder.Append("GuestId=" + model.GuestId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("AllMoney=" + model.AllMoney + ",");
            builder.Append("AccountId=" + model.AccountId + ",");
            builder.Append("PayTime='" + model.PayTime + "',");
            builder.Append("CreateMan=" + model.CreateMan + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("BallotType=" + model.BallotType + ",");
            builder.Append("PayType=" + model.PayType + ",");
            builder.Append("Cost=" + model.Cost + ",");
            builder.Append("TIN='" + model.TIN + "',");
            builder.Append("BankName='" + model.BankName + "',");
            builder.Append("BankNum='" + model.BankNum + "',");
            builder.Append("IsInvoicing=" + model.IsInvoicing + ",");
            builder.Append("InvoicingNum='" + model.InvoicingNum + "',");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where PaymentId=" + model.PaymentId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

